class DocumentVersion < ActiveRecord::Base 
  belongs_to :document

  file_column(:name, :store_dir => :location)
  validates_file_format_of(:name, :in => %w(doc pdf xls txt swf tiff))

  validates_presence_of(:name,:location,:document_id,:updated_by)

  before_save :assign_file_size


  def make_current
    DocumentVersion.update_records_by_keys([{"document_id" => document.id}],{"is_current" => false})
    DocumentVersion.update_record_by_keys([{"document_id" => document.id, "id" => self.id}],{"is_current" => true})
  end

  def assign_file_size
   self.kilobytes = ((File.size(self.name) / 1024) * 100).round / 100.0
  end
end
